/*
 Copyright 2011, Lucid Technics, LLC.

 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in
 writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 specific language governing permissions and limitations under the License.
*/

package airlift.generator;

import org.antlr.stringtemplate.StringTemplate;

import javax.lang.model.element.Element;
import java.util.Iterator;
import java.util.Map;

// TODO: Auto-generated Javadoc
/**
 * The Class SqlGenerator.
 */
public class SqlGenerator
   extends Generator
{
    
    /** The comment. */
    public String comment = "--This SQL code has been generated by airlift. Do not modify this code.";

    /** The Constant AIRLIFT_SQL_SELECT_SQLTEMPLATE. */
    protected static final String AIRLIFT_SQL_SELECT_SQLTEMPLATE = "airlift/sql/SelectSQLTemplate";
    
    /** The Constant AIRLIFT_SQL_FIND_BY_RANGE_SQLTEMPLATE. */
    protected static final String AIRLIFT_SQL_FIND_BY_RANGE_SQLTEMPLATE = "airlift/sql/FindByRangeSQLTemplate";
    
    /** The Constant AIRLIFT_SQL_FIND_BY_ATTRIBUTE_SQLTEMPLATE. */
    protected static final String AIRLIFT_SQL_FIND_BY_ATTRIBUTE_SQLTEMPLATE = "airlift/sql/FindByAttributeSQLTemplate";
    
    /** The Constant AIRLIFT_SQL_COLLECT_FOR_CHANGE_FEED_SQLTEMPLATE. */
    protected static final String AIRLIFT_SQL_COLLECT_FOR_CHANGE_FEED_SQLTEMPLATE = "airlift/sql/CollectForChangeFeedSQLTemplate";
    
    /** The Constant AIRLIFT_SQL_FIND_SQLTEMPLATE. */
    protected static final String AIRLIFT_SQL_FIND_SQLTEMPLATE = "airlift/sql/FindSQLTemplate";

    /**
     * Instantiates a new sql generator.
     */
    public SqlGenerator() {}
	
    /* (non-Javadoc)
     * @see airlift.generator.Generator#generate(java.lang.String, java.lang.String, javax.lang.model.element.Element, airlift.generator.DomainObjectModel, java.util.Map)
     */
    public void generate(String _appName,
			String _directory,
			Element _element,
			DomainObjectModel _domainObjectModel,
			Map<String, DomainObjectModel> _elementNameToDomainObjectModelMap)
	{
		String generatedString = generateSelectSql(_domainObjectModel);
		String fileName =  _domainObjectModel.getClassName() + "/select" + _domainObjectModel.getClassName() + ".sql";
		writeResourceFile(fileName, _directory, generatedString, _element);

		generatedString = generateFindSql(_domainObjectModel);
		fileName =  _domainObjectModel.getClassName() + "/find" + _domainObjectModel.getClassName() + ".sql";
		writeResourceFile(fileName, _directory, generatedString, _element);

		generatedString = generateExistsSql(_domainObjectModel);
		fileName =  _domainObjectModel.getClassName() + "/exists" + _domainObjectModel.getClassName() + ".sql";
		writeResourceFile(fileName, _directory, generatedString, _element);
	}

	/**
	 * Generate select sql.
	 *
	 * @param _domainObjectModel the _domain object model
	 * @return the string
	 */
	public String generateSelectSql(DomainObjectModel _domainObjectModel)
	{
		StringTemplate stringTemplate = getStringTemplateGroup().getInstanceOf(AIRLIFT_SQL_SELECT_SQLTEMPLATE);

		Iterator attributes = _domainObjectModel.getAttributes();
		String tableName = _domainObjectModel.getTableName();

		stringTemplate.setAttribute("tableName", tableName);

		return stringTemplate.toString();
	}

	/**
	 * Generate find by this range sql.
	 *
	 * @param _domainObjectModel the _domain object model
	 * @param _rangeField the _range field
	 * @return the string
	 */
	public String generateFindByThisRangeSql(DomainObjectModel _domainObjectModel, String _rangeField)
	{
	    StringTemplate stringTemplate = getStringTemplateGroup().getInstanceOf(AIRLIFT_SQL_FIND_BY_RANGE_SQLTEMPLATE);
	    Iterator attributes = _domainObjectModel.getAttributes();
	    String tableName = _domainObjectModel.getTableName();

		stringTemplate.setAttribute("rangeField", _rangeField);
	    stringTemplate.setAttribute("tableName", tableName);

	    return stringTemplate.toString();
	}

	/**
	 * Generate find by this attribute sql.
	 *
	 * @param _domainObjectModel the _domain object model
	 * @param _attribute the _attribute
	 * @return the string
	 */
	public String generateFindByThisAttributeSql(DomainObjectModel _domainObjectModel, String _attribute)
	{
	    StringTemplate stringTemplate = getStringTemplateGroup().getInstanceOf("airlift/sql/FindByAttributeSQLTemplate");

	    Iterator attributes = _domainObjectModel.getAttributes();
	    String tableName = _domainObjectModel.getTableName();

		stringTemplate.setAttribute("attribute", _attribute);
	    stringTemplate.setAttribute("tableName", tableName);

	    return stringTemplate.toString();
	}

	/**
	 * Generate find keys sql.
	 *
	 * @param _domainObjectModel the _domain object model
	 * @return the string
	 */
	public String generateFindKeysSql(DomainObjectModel _domainObjectModel)
	{
		StringTemplate stringTemplate = getStringTemplateGroup().getInstanceOf("airlift/sql/FindKeysSQLTemplate");

		Iterator attributes = _domainObjectModel.getAttributes();
		String tableName = _domainObjectModel.getTableName();

		stringTemplate.setAttribute("package", _domainObjectModel.getRootPackageName());
		stringTemplate.setAttribute("className", upperTheFirstCharacter(_domainObjectModel.getClassName()));

		return stringTemplate.toString();
	}

	/**
	 * Generate find by this membership sql.
	 *
	 * @param _domainObjectModel the _domain object model
	 * @param _attribute the _attribute
	 * @return the string
	 */
	public String generateFindByThisMembershipSql(DomainObjectModel _domainObjectModel, String _attribute)
	{
		StringTemplate stringTemplate = getStringTemplateGroup().getInstanceOf("airlift/sql/FindByMembershipSQLTemplate");

		Iterator attributes = _domainObjectModel.getAttributes();
		String tableName = _domainObjectModel.getTableName();

		stringTemplate.setAttribute("tableName", tableName);

		return stringTemplate.toString();
	}

	/**
	 * Generate find sql.
	 *
	 * @param _domainObjectModel the _domain object model
	 * @return the string
	 */
	public String generateFindSql(DomainObjectModel _domainObjectModel)
	{
		StringTemplate stringTemplate = getStringTemplateGroup().getInstanceOf(AIRLIFT_SQL_FIND_SQLTEMPLATE);

		Iterator attributes = _domainObjectModel.getAttributes();
		String tableName = _domainObjectModel.getTableName();
		
		stringTemplate.setAttribute("tableName", tableName);

		return stringTemplate.toString();
	}

	/**
	 * Generate exists sql.
	 *
	 * @param _domainObjectModel the _domain object model
	 * @return the string
	 */
	public String generateExistsSql(DomainObjectModel _domainObjectModel)
	{
		return generateFindSql(_domainObjectModel);
	}
}